import 'package:flutter/material.dart';
import '../code/observable_field.dart';

///@author sunlunchang
///加载对话框
class LoadingDialog extends Dialog {
  final String? text;

  LoadingDialog({Key? key, this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LoadingView(text: text, materialType: MaterialType.transparency);
  }

  ///显示对话框
  static Future<dynamic> showLoadingDialog(BuildContext context,
      {bool barrierDismissible = false, String? text}) {
    return showDialog(
        context: context,
        barrierDismissible: barrierDismissible,
        builder: (context) {
          return LoadingDialog(text: text);
        });
  }

  ///关闭对话框
  static void dismissLoadingDialog(BuildContext context) {
    Navigator.pop(context);
  }
}

/// loading 控件
class LoadingView extends StatelessWidget {
  final String? text;

  final MaterialType? materialType;

  LoadingView({this.text, this.materialType});

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return Material(
      type: materialType ?? MaterialType.canvas,
      child: new Center(
        child: new ConstrainedBox(
          constraints: BoxConstraints(
              minWidth: 120, maxWidth: 200, minHeight: 120, maxHeight: 200),
          child: Ink(
              decoration: BoxDecoration(
                color: themeData.dialogBackgroundColor,
                borderRadius: BorderRadius.all(Radius.circular(4)),
              ),
              child: new Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  new CircularProgressIndicator(),
                  new Padding(
                      padding: EdgeInsets.only(
                        top: text != null ? 16 : 0,
                      ),
                      child: text == null
                          ? null
                          : new Text(text!,
                              style: themeData.dialogTheme.titleTextStyle)),
                ],
              )),
        ),
      ),
    );
  }
}

///loading dialog的控件
///使用混入引用
///@author sunlunchang
class LoadingDialogWidgetVd {
  void registerDialogEvent(BuildContext context, LoadingDialogVmSub vmSub) {
    vmSub.setShowLoadingOfFunc((text) {
      return showLoadingDialog(context, text: text);
    });
    vmSub.dismissLoadingOf.addListener(() {
      dismissLoadingDialog(context);
    });
  }

  ///显示对话框
  Future<dynamic> showLoadingDialog(BuildContext context,
      {bool barrierDismissible = false, String? text}) {
    return LoadingDialog.showLoadingDialog(context,
        barrierDismissible: barrierDismissible, text: text);
  }

  ///关闭对话框
  void dismissLoadingDialog(BuildContext context) {
    Navigator.pop(context);
  }
}

///loading dialog的VM
///使用混入引用
///@author sunlunchang

abstract class ILoadingDialogVmSub {
  Future<dynamic> showLoading({String? text});

  dismissLoading();
}

class LoadingDialogVmSub implements ILoadingDialogVmSub {
  ///显示对话框
  late Future<dynamic> Function(String?)? _showLoadingOfFunc;

  final ObservableField<String> dismissLoadingOf =
      ObservableField(single: true);

  void setShowLoadingOfFunc(
      Future<dynamic> Function(String?)? showLoadingOfFunc) {
    this._showLoadingOfFunc = showLoadingOfFunc;
  }

  @override
  Future<dynamic> showLoading({String? text}) {
    if (_showLoadingOfFunc != null) {
      return _showLoadingOfFunc!.call(text);
    } else {
      throw StateError('showLoadingOfFunc is not initialized');
    }
  }

  @override
  dismissLoading() {
    dismissLoadingOf.notifyListeners();
  }
}
